//
//  BmobTools.swift
//  FiveProvincesOfNorthChina
//
//  Created by kys-20 on 2019/9/13.
//  Copyright © 2019 kys-20. All rights reserved.
//


let grayColor = UIColor(red: 240/255, green: 240/255, blue: 240/255, alpha: 1)
let blueColor = UIColor(red: 78/255, green: 192/255, blue: 255/255, alpha: 1)

let domainName = "http://q24x1vc9d.bkt.clouddn.com/"
public typealias BmobSuccess = (_ str:String) -> ()
public typealias BmobFailure = (_ str:String) -> ()
public typealias BmobSuccessAny = (_ arr:[Any],_ count:Int) -> ()
//public typealias intBmobSuccess = (_ count:Int) -> ()
//public typealias BmobFailure = (_ error:Error) -> ()
//public typealias BmobIsHaveUserName = (_ response : BmobObject) -> ()
//public typealias getDataArray = (_ array:Array<Any>) -> ()
//public typealias getDataArrayFailure = (_ error:Error) -> ()
//public typealias userLoginFailure =  (_ str:String) -> ()
//
//public typealias queuingInformationQuery = (_ strArray:[String]) -> ()

import UIKit
import Foundation

class BmobTools {
    
    
    private static let singleton = BmobTools()
    class func shareInstance() -> BmobTools{
        return singleton
    }
    private init() {
    }
}

extension BmobTools{


    /// 更新数据（用户自己）
    ///
    /// - Parameters:
    ///   - nameOfTheTable: 表名
    ///   - columnName: 列名
    ///   - value: 值
    ///   - success: <#success description#>
    ///   - failure: <#failure description#>
    class func updateTheData(nameOfTheTable:String,columnName:String,value:String,success: @escaping BmobSuccess,failure : @escaping BmobFailure){
    
        let batch = BmobObjectsBatch()
        batch.updateBmobObject(withClassName: nameOfTheTable, objectId:UserDefaults.standard.string(forKey: "objectId"), parameters: [columnName:value])
        batch.batchObjectsInBackground { (isSuccessful, error) in
            if error != nil {
                failure("\(String(describing: error))")
            }else{
                success("success")
            }
        }
//        let objectUser = BmobObject(className: nameOfTheTable)
//        if let object = objectUser {
//
//
//
//        }
        
    }

    
    
    /// 单条语句查询
    ///
    /// - Parameters:
    ///   - nameOfTheTable: 表名
    ///   - columnName: 列名
    ///   - value: 值
    ///   - success: <#success description#>
    ///   - failure: <#failure description#>
    class func ComparisonOfTheQuery(nameOfTheTable:String,columnName:String,value:String,success:@escaping BmobSuccessAny,failure : @escaping BmobFailure){
        let query : BmobQuery = BmobQuery(className: nameOfTheTable)
        query.whereKey(columnName, equalTo: value)
        let main : BmobQuery =  BmobQuery(className: nameOfTheTable)
        main.add(query)
        main.andOperation()
        main.findObjectsInBackground{ (array,error) in
            if error != nil{
                failure("\(String(describing: error))")
            }else{
                success(array!,array!.count)
            }
            
        }
    }
    
    /// 条件查询传出数据的数量
    ///
    /// - Parameters:
    ///   - nameOfTheTable: 表名
    ///   - columnName: 列名数组
    ///   - value: 值数组
    ///   - success: <#success description#>
    ///   - failure: <#failure description#>
    class func conditionsOfTheQuery(nameOfTheTable:String,columnName:[String],value:[String],success:@escaping BmobSuccess,failure : @escaping BmobFailure){
        let query : BmobQuery = BmobQuery(className: nameOfTheTable)
          let arra = [[columnName[0]:value[0]],[columnName[1]:value[1]]]
        query.addTheConstraintByAndOperation(with: arra)
        let main : BmobQuery =  BmobQuery(className: nameOfTheTable)
        main.add(query)
        main.andOperation()
        main.findObjectsInBackground{ (array,error) in
            if error != nil{
                failure("\(String(describing: error))")
            }
            if array?.count == 0{
                success("0")
            }else{
                success("\(String(describing: array?.count))")
            }
        }
    }
 
    
    /// 添加好友
    ///
    /// - Parameters:
    ///   - nameOfTheTable: 表名
    ///   - objectId: <#objectId description#>
    ///   - columnName: 列名
    ///   - value: <#value description#>
    ///   - success: <#success description#>
    ///   - failure: <#failure description#>
    class func addFriendsBmob(nameOfTheTable:String,objectId:String,columnName:String,value:String,success:@escaping BmobSuccess,failure : @escaping BmobFailure){
        let query = BmobObject(outDataWithClassName: nameOfTheTable, objectId: objectId)
//        query?.addObjects(from: [value], forKey: columnName)
        query?.addUniqueObjects(from: [value], forKey: columnName)
        query?.updateInBackground(resultBlock: { (isSuccessful, error) in
            if error == nil{
                success("success")
            }else{
                failure("\(String(describing: error))")
                print("\(String(describing: error))")
            }
        })
    }
    
    /// 删除好友
    ///
    /// - Parameters:
    ///   - nameOfTheTable: <#nameOfTheTable description#>
    ///   - objectId: <#objectId description#>
    ///   - columnName: <#columnName description#>
    ///   - value: <#value description#>
    ///   - success: <#success description#>
    ///   - failure: <#failure description#>
    class func deleteFriendsBmob(nameOfTheTable:String,objectId:String,columnName:String,value:String,success:@escaping BmobSuccess,failure : @escaping BmobFailure){
        let query = BmobObject(outDataWithClassName: nameOfTheTable, objectId: objectId)
        query?.removeObjects(in: [value], forKey: columnName)
        query?.updateInBackground(resultBlock: { (isSuccessful, error) in
            if error == nil{
                success("success")
            }else{
                failure("\(String(describing: error))")
            }
        })
    }
  
}
